Skip to main content

Mongoose Plugin for Godspeed

It provides seamless integration with MongoDB through the Mongoose library. MongoDB is a popular NoSQL database, and with this plugin, you can harness the power of Mongoose to model your data, perform queries, and interact with MongoDB in a structured and efficient manner.

How to Add

Create a godspeed project from the CLI, open the created project in vscode and then add the plugin:

godspeed plugin add @godspeedsystems/plugins-mongoose-as-datasource 

**- ** You will find two files in your project related to the mongoose plugin at src/datasources/types/mongoose.ts and src/datasources/mongoose.yaml

import { DataSource } from '@godspeedsystems/plugins-mongoose-as-datastore';
export default DataSource;
type: mongoose
successResponseCodes: #default response codes for success responses
create: 201
find: 200
findOne: 200
aggregate: 200
findOneAndUpdate: 201
findOneAndDelete: 202

Alt text

  • You can keep the file by any name. This file is used to initialize a mongoose datasource instance. Whatever is the name of the file, you will need to invoke the mongoose datasource commands by the same name. Also your models will be needed to be kept in a folder with the same name as your yaml file (i.e. your datasource instance name). For example mongoose1.yaml would mean calling fn:datasources.mongoose1.<Model_Name>.<Function_Name> from yaml workflows and ctx.datasources.mongoose1.<Model_Name>.<Function_Name> from TS/JS files. Also you will need to create a folder datasources/mongoose1/models and keep your models there as detailed below.

  • You can override the default response codes for success cases for different methods by putting them in the datasource instance's yaml file

Provide Connection URL

Set an environment variable named MONGO_URL as your connection string to running mongodb instance. You can save url in .env file as

  MONGO_URL='mongodb+srv://<user_name>:<password>@cluster0.xyzabc.mongodb.net/?retryWrites=true&w=majority'

Setting up Mongoose models

This datasource loads the Mongoose models from datasources/<datasource_name>/models folder.

Alt text

These files are stored in datasources/<datasource_name>/models folder.

Writing Mongoose Models in Godspeed

Godspeed framework uses CommonJS module syntax. When defining Mongoose models, ensure you follow the below format:

Import Mongoose: Use the require syntax to import Mongoose:

const { model, Schema, Document } = require('mongoose');

Export the Model: Export the model using the module.exports syntax. The export should include a type property (used for accessing the model) and the Mongoose model instance itself.

module.exports = {
type: 'SomeModel', // The name by which you will access methods of this collection/model
model: SomeModel // The Mongoose Model
};

An example Mongoose model file


const { model, Schema } =require('mongoose');

const participantSchema = new Schema({
participant_id: { type: Number, required: true, unique: true },
name: { type: String, required: true },
email: { type: String, required: true },
region: { type: String, required: true },
city: { type: String, required: true }
});

const Participant = model('Participant', participantSchema);
module.exports = {
type: 'Participant', // The name by which you will access methods of this collection/model
model: Participant // The Mongoose Model
};

Sample Event and workflow

Event

http.post./participant:
fn: createParticipant
body:
content:
application/json:
schema:
type: object
properties:
participant_id:
type: number
name:
type: string
description: Name
email:
type: string
description: Email id
region:
type: string
responses:
201:
description: Participant created successfully
content:
application/json:
schema:
type: object
properties:
someModel:
type: object
description: The created Participant object

Workflow

When calling any api function it will be called as ctx.datasources.mongoose1.<Model_Name>.<Function_Name> from TS/JS files.

Create

import { GSContext, GSDataSource, GSStatus } from "@godspeedsystems/core";

export default async function (ctx: GSContext) {

const mongoClient: GSDataSource = ctx.datasources.mongoose;
const { body }= ctx.inputs.data ;
const data = {
meta: {
entityType: 'Participant',
method: 'create'
},
...body
};

const response = await mongoClient.execute(ctx, data);
return response;
}

Find

import { GSContext, GSDataSource, GSStatus } from "@godspeedsystems/core";

export default async function (ctx: GSContext){
const { query } = ctx.inputs.data; // for GET request, query params are here
const { email } = query;

const mongoClient: GSDataSource = ctx.datasources.mongoose;

try {
const result = await mongoClient.execute(ctx, {
meta: {
entityType: 'Participant',
method: 'findOne'
},
email: email
});

return result;
} catch (error: any) {
ctx.childLogger.error("Failed to fetch user by email: %o", error);
return new GSStatus(false, 500, "Internal Server Error", error);
}
}

Error response

When a call has an error the datasource returns following GSStatus.

    code: 500
success: false
data:
message: Internal Server Error

Run the service

  • From your command line run your service in the auto-watch mode
    godspeed serve

**- ** Plugin Repository
**- ** Issue Tracker
**- ** npm package